package cn.feng.thinkInJava.a8_2_字符串.strings.a13_06_06;

//: strings/StartEnd.java
import java.util.regex.*;
import static net.mindview.util.Print.*;

/**
 * int start(int group)：返回前一次匹配操作期间指定的组所匹配的子序列的初始索引。 int end(int
 * group)：返回前一次匹配操作期间指定的组所匹配的子序列的最后索引+1。
 */
public class StartEnd
{
	public static String input="As long as there is injustice, whenever a\n"+"Targathian baby cries out, wherever a distress\n"
			+"signal sounds among the stars ... We'll be there.\n"+"This fine ship, and this fine crew ...\n"+"Never give up! Never surrender!";
	private static class Display
	{
		private boolean regexPrinted=false;
		private String regex;
		
		Display(String regex)
		{
			this.regex=regex;
		}
		
		void display(String message)
		{
			if(!regexPrinted)
			{
				print(regex);
				regexPrinted=true;
			}
			print(message);
		}
	}
	
	static void examine(String s,String regex)
	{
		Display d=new Display(regex);
		Pattern p=Pattern.compile(regex);
		Matcher m=p.matcher(s);
		while(m.find())
			d.display("find() '"+m.group()+"' start = "+m.start()+" end = "+m.end());
		if(m.lookingAt()) // No reset() necessary
			d.display("lookingAt() start = "+m.start()+" end = "+m.end());
		if(m.matches()) // No reset() necessary
			d.display("matches() start = "+m.start()+" end = "+m.end());
	}
	
	public static void main(String[] args)
	{
		for(String in:input.split("\n"))
		{
			print("input : "+in);
			for(String regex:new String[] { "\\w*ere\\w*", "\\w*ever", "T\\w+", "Never.*?!" })
				examine(in,regex);
		}
	}
} /* Output:
input : As long as there is injustice, whenever a
\w*ere\w*
find() 'there' start = 11 end = 16
\w*ever
find() 'whenever' start = 31 end = 39
input : Targathian baby cries out, wherever a distress
\w*ere\w*
find() 'wherever' start = 27 end = 35
\w*ever
find() 'wherever' start = 27 end = 35
T\w+
find() 'Targathian' start = 0 end = 10
lookingAt() start = 0 end = 10
input : signal sounds among the stars ... We'll be there.
\w*ere\w*
find() 'there' start = 43 end = 48
input : This fine ship, and this fine crew ...
T\w+
find() 'This' start = 0 end = 4
lookingAt() start = 0 end = 4
input : Never give up! Never surrender!
\w*ever
find() 'Never' start = 0 end = 5
find() 'Never' start = 15 end = 20
lookingAt() start = 0 end = 5
Never.*?!
find() 'Never give up!' start = 0 end = 14
find() 'Never surrender!' start = 15 end = 31
lookingAt() start = 0 end = 14
matches() start = 0 end = 31
*///:~
